package com.kgc.javaweb.service.impl;


import com.kgc.javaweb.dao.HouseDao;
import com.kgc.javaweb.dao.impl.HouseDaoImpl;
import com.kgc.javaweb.dto.UserPageDTO;
import com.kgc.javaweb.entity.House;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.service.HouseService;
import com.kgc.javaweb.util.ResponseResult;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class HouseServiceImpl implements HouseService {

    private HouseDao houseDao = new HouseDaoImpl();

    @Override//用、用、用
    public ResponseResult selectPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<House> houses =  houseDao.selectPage(userPageDTO);
            // 查询数据总条数
            Long  total =  houseDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(houses);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用、用
    public ResponseResult selectById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            House house =  houseDao.selectById(id);
            responseResult.setData(house);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用、用
    public ResponseResult update(House hous,Integer id) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            houseDao.update(hous,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用、用
    public ResponseResult add(House hous) {

        ResponseResult responseResult = ResponseResult.builder().build();
            try {
                houseDao.add(hous);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }

        return responseResult;
    }

    @Override//用、用、用
    public ResponseResult delete(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            houseDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }
}
